<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Executes a statement prepared with sqlsrv_prepare</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.sqlsrv-execute" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">sqlsrv_execute</h1>
  <p class="verinfo">(No version information available, might only be in Git)</p><p class="refpurpose"><span class="refname">sqlsrv_execute</span> &mdash; <span class="dc-title">Executes a statement prepared with <span class="function"><a href="sqlsrv_prepare.html" class="function">sqlsrv_prepare()</a></span></span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.sqlsrv-execute-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>sqlsrv_execute</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$stmt</code></span>
   ) : <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   Executes a statement prepared with <span class="function"><a href="sqlsrv_prepare.html" class="function">sqlsrv_prepare()</a></span>. This 
   function is ideal for executing a prepared statement multiple times with 
   different parameter values.
  </p>
 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.sqlsrv-execute-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">stmt</code></dt>

     <dd>

      <p class="para">
       A statement resource returned by <span class="function"><a href="sqlsrv_prepare.html" class="function">sqlsrv_prepare()</a></span>.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.sqlsrv-execute-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-function.sqlsrv-execute-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2575">
    <p><strong>Example #1 <span class="function"><strong>sqlsrv_execute()</strong></span> example</strong></p>
    <div class="example-contents"><p>
    This example demonstrates how to prepare a statement with 
    <span class="function"><a href="sqlsrv_prepare.html" class="function">sqlsrv_prepare()</a></span> and re-execute it multiple times (with 
    different parameter values) using <span class="function"><strong>sqlsrv_execute()</strong></span>.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$serverName&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"serverName\sqlexpress"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$connectionInfo&nbsp;</span><span style="color: #007700">=&nbsp;array(&nbsp;</span><span style="color: #DD0000">"Database"</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"dbName"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"UID"</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"username"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"PWD"</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"password"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sqlsrv_connect</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$serverName</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$connectionInfo</span><span style="color: #007700">);<br />if(&nbsp;</span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">sqlsrv_errors</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #0000BB">$sql&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"UPDATE&nbsp;Table_1<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SET&nbsp;OrderQty&nbsp;=&nbsp;?<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;SalesOrderID&nbsp;=&nbsp;?"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Initialize&nbsp;parameters&nbsp;and&nbsp;prepare&nbsp;the&nbsp;statement.&nbsp;<br />//&nbsp;Variables&nbsp;$qty&nbsp;and&nbsp;$id&nbsp;are&nbsp;bound&nbsp;to&nbsp;the&nbsp;statement,&nbsp;$stmt.<br /></span><span style="color: #0000BB">$qty&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$id&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sqlsrv_prepare</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$sql</span><span style="color: #007700">,&nbsp;array(&nbsp;&amp;</span><span style="color: #0000BB">$qty</span><span style="color: #007700">,&nbsp;&amp;</span><span style="color: #0000BB">$id</span><span style="color: #007700">));<br />if(&nbsp;!</span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">sqlsrv_errors</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Set&nbsp;up&nbsp;the&nbsp;SalesOrderDetailID&nbsp;and&nbsp;OrderQty&nbsp;information.&nbsp;<br />//&nbsp;This&nbsp;array&nbsp;maps&nbsp;the&nbsp;order&nbsp;ID&nbsp;to&nbsp;order&nbsp;quantity&nbsp;in&nbsp;key=&gt;value&nbsp;pairs.<br /></span><span style="color: #0000BB">$orders&nbsp;</span><span style="color: #007700">=&nbsp;array(&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">10</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">20</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">30</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Execute&nbsp;the&nbsp;statement&nbsp;for&nbsp;each&nbsp;order.<br /></span><span style="color: #007700">foreach(&nbsp;</span><span style="color: #0000BB">$orders&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$id&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$qty</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Because&nbsp;$id&nbsp;and&nbsp;$qty&nbsp;are&nbsp;bound&nbsp;to&nbsp;$stmt1,&nbsp;their&nbsp;updated<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;values&nbsp;are&nbsp;used&nbsp;with&nbsp;each&nbsp;execution&nbsp;of&nbsp;the&nbsp;statement.&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;</span><span style="color: #0000BB">sqlsrv_execute</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">)&nbsp;===&nbsp;</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die(&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">sqlsrv_errors</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.sqlsrv-execute-notes">
  <h3 class="title">注释</h3>
  <p class="para">
   When you prepare a statement that uses variables as parameters, the variables 
   are bound to the statement. This means that if you update the values of the 
   variables, the next time you execute the statement it will run with updated 
   parameter values. For statements that you plan to execute only once, use 
   <span class="function"><a href="sqlsrv_query.html" class="function">sqlsrv_query()</a></span>.
  </p>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.sqlsrv-execute-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="sqlsrv_prepare.html" class="function" rel="rdfs-seeAlso">sqlsrv_prepare()</a> - Prepares a query for execution</span></li>
    <li class="member"><span class="function"><a href="sqlsrv_query.html" class="function" rel="rdfs-seeAlso">sqlsrv_query()</a> - Prepares and executes a query</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>